package cn.proj.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.proj.domain.UserBean;

public class UserDao {

	private static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
	private static final String USERNSME = "panke";
	private static final String PASSWORD = "123";
	private Connection con = null;
	PreparedStatement pst = null;
	ResultSet rs = null;

	public UserBean userLogin(UserBean user) {

		String sql = "select * from im_users where username=? and password=?";
		try {
			con = DriverManager.getConnection(URL, USERNSME, PASSWORD);
			pst = con.prepareStatement(sql);
			pst.setString(1, user.getUsername());
			pst.setString(2, user.getPassword());
			rs = pst.executeQuery();
			System.out.println(user.getUsername());
			System.out.println(user.getPassword());
			if (rs.next()) {
				UserBean u = new UserBean();
				u.setUsername(rs.getString("username"));
				u.setPassword(rs.getString("password"));
				System.out.println(rs.getString("username"));
				System.out.println(rs.getString("password"));
				return u;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

			try {
				if (pst != null) {
					pst.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (con != null) {
					con.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
